import {
  eolink, R as _R
} from '@/api'
import { StatusCode } from '@/utils/eolink'
import axios from 'axios'
import _ from 'lodash'
import { rest } from 'msw'
import R from './_util'

const cbase = R.cbase('/eolink')

axios.defaults.withCredentials = true

/** 转发获取项目信息 */
export const getProjectInfo = rest[R.method(eolink.getProjectInfo)](R.base(eolink.getProjectInfo), async (req, res, ctx) => {
  const vo = await axios({
    method: R.method(eolink.getProjectInfo),
    url: cbase(eolink.getProjectInfo),
    data: await req.text(),
    headers: _.fromPairs([...req.headers.entries()])
  })
  return res(ctx.json(vo))
})

export const verifySharePassword = rest[R.method(eolink.verifySharePassword)](R.base(eolink.verifySharePassword), async (req, res, ctx) => {
  const vo: _R.IResponse<typeof eolink.verifySharePassword> = await axios({
    method: R.method(eolink.verifySharePassword),
    url: cbase(eolink.verifySharePassword),
    data: await req.text(),
    headers: _.fromPairs([...req.headers.entries()])
  }) as any
  return res(ctx.json(vo), ctx.status(vo.statusCode == StatusCode.OK ? 200 : 400))
})

export const verifyShareToken = rest[R.method(eolink.verifyShareToken)](R.base(eolink.verifyShareToken), async (req, res, ctx) => {
  const vo: _R.IResponse<typeof eolink.verifySharePassword> = await axios({
    method: R.method(eolink.verifyShareToken),
    url: cbase(eolink.verifyShareToken),
    data: await req.text(),
    headers: _.fromPairs([...req.headers.entries()])
  }) as any
  return res(ctx.json(vo), ctx.status(vo.statusCode == StatusCode.OK ? 200 : 400))
})

export const getApiGroupData = rest[R.method(eolink.getApiGroupData)](R.base(eolink.getApiGroupData), async (req, res, ctx) => {
  return res(ctx.json((await import('./getApiGroupData.json')).default))
})

export const getApiList = rest[R.method(eolink.getApiList)](R.base(eolink.getApiList), async (req, res, ctx) => {
  const vo: _R.IResponse<typeof eolink.getApiList> = await axios({
    method: R.method(eolink.getApiList),
    url: cbase(eolink.getApiList),
    data: await req.text(),
  }) as any
  // const vo = (await import('./getApiList.json')).default
  return res(ctx.json(vo), ctx.status(vo.statusCode == StatusCode.OK ? 200 : 400))
})

export const searchApi = rest[R.method(eolink.searchApi)](R.base(eolink.searchApi), async (req, res, ctx) => {
  const vo: _R.IResponse<typeof eolink.searchApi> = await axios({
    method: R.method(eolink.searchApi),
    url: cbase(eolink.searchApi),
    data: await req.text(),
    headers: _.fromPairs([...req.headers.entries()])
  }) as any
  return res(ctx.json(vo), ctx.status(vo.statusCode == StatusCode.OK ? 200 : 400))
})

export const getApiInfo = rest[R.method(eolink.getApiInfo)](R.base(eolink.getApiInfo), async (req, res, ctx) => {
  const vo = await axios({
    method: R.method(eolink.getApiInfo),
    url: cbase(eolink.getApiInfo),
    data: await req.text(),
    headers: _.fromPairs([...req.headers.entries()])
  })
  // const vo = (await import('./getApiInfo.json')).default
  return res(ctx.json(vo))
})